﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="XuatBanHang.ascx.cs" Inherits="XuatBanHang" %>
<asp:Panel runat="server" ID="pnBanHang">
    <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td colspan="2">
                <h2 align="center" style="display: none;">
                    PHIẾU BÁN HÀNG LẺ
                </h2>
            </td>
        </tr>
        <tr>
            <td colspan="2" valign="middle" align="center">
                Mã số/Mã vạch:
                <asp:TextBox runat="server" ID="txtMa"></asp:TextBox>
                Số lượng:
                <asp:TextBox runat="server" ID="txtSoLuong" Text="1" Enabled="False" Width="30px"></asp:TextBox>
                <asp:Button runat="server" ID="cmdThem" Text="Thêm" Width="80px" OnClick="cmdThem_Click" />
            </td>
        </tr>
        <tr>
            <td width="200" valign="top">
                <table width="100%">
                    <tr>
                        <td width="80px" nowrap align="left">
                            Cộng tiền:
                        </td>
                        <td align="left">
                            <asp:TextBox runat="server" ID="txtCongTien" BackColor="#FFFFCC" BorderColor="#000066"
                                BorderStyle="Solid" Font-Bold="True" ForeColor="Blue" Width="110px" ReadOnly="True"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            Khách hàng:
                        </td>
                        <td align="left">
                            <asp:DropDownList Width="110px" runat="server" ID="cboKhachHang" DataTextField="Ten"
                                DataValueField="Id">
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" colspan="2">
                            Nội dung:<br />
                            <asp:TextBox ID="txtNoiDung" runat="server" TextMode="MultiLine" Width="99%"></asp:TextBox>
                        </td>
                    </tr>
                </table>
                <br />
            </td>
            <td width="500" valign="top" style="padding-left:10px;">
                <asp:DataGrid ID="detailGrid" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                    OnPageIndexChanged="detailGrid_PageIndexChanged" Width="100%" DataSourceID="dsDetail"
                    OnItemCommand="detailGrid_ItemCommand" OnItemDataBound="detailGrid_ItemDataBound">
                    <PagerStyle Mode="NumericPages" />
                    <AlternatingItemStyle CssClass="DarkRow" />
                    <ItemStyle CssClass="LightRow" />
                    <Columns>
                        <asp:BoundColumn Visible="false" HeaderText="id" DataField="id"></asp:BoundColumn>
                        <asp:BoundColumn HeaderText="Mã hàng" DataField="Seri"></asp:BoundColumn>
                        <asp:BoundColumn HeaderText="Tên hàng" DataField="TenHang"></asp:BoundColumn>
                        <asp:BoundColumn HeaderText="Số lượng" DataField="SoLuong"></asp:BoundColumn>
                        <asp:BoundColumn HeaderText="Đơn giá" DataField="GiaBanThamkhao"></asp:BoundColumn>
                        <asp:BoundColumn HeaderText="Chiết khấu (%)" DataField="ChietKhau"></asp:BoundColumn>
                        <asp:BoundColumn HeaderText="Thành tiền" DataField="GiaBanThucTe"></asp:BoundColumn>
                        <asp:TemplateColumn>
                            <ItemTemplate>
                                <asp:Button CausesValidation="False" ID="cmdCK" runat="server" Text="Chiết khấu" Width="80"
                                    CommandName="cmdCK">
                                </asp:Button>
                            </ItemTemplate>
                        </asp:TemplateColumn>
                        <asp:TemplateColumn>
                            <ItemTemplate>
                                <asp:Button CausesValidation="False" ID="cmdDelete" runat="server" Text="Xóa" Width="40"
                                    CommandName="cmdDelete" OnClientClick="return confirm('Ban co chac chan muon xoa?');">
                                </asp:Button>
                            </ItemTemplate>
                        </asp:TemplateColumn>
                    </Columns>
                    <HeaderStyle CssClass="HeaderRow" Font-Bold="True" />
                </asp:DataGrid>
                <br />
                <table runat="server" id="pnCK" visible="false" width="300" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>Mã số</td>
                        <td><asp:Label runat="server" ID="lblSeri"></asp:Label></td>
                    </tr>
                    <tr>
                        <td>
                            Giá ban đầu:</td>
                        <td>
                            <asp:TextBox runat="server" ID="txtGiaGoc" ReadOnly="true"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Nhập chiết khấu:</td>
                        <td>
                            <asp:TextBox runat="server" ID="txtCK"></asp:TextBox>
                            %</td>
                    </tr>
                    <tr>
                        <td>
                            Giá bán:</td>
                        <td>
                            <asp:TextBox runat="server" ID="txtGiaBan"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <br />
                            <asp:Button runat="server" ID="cmdSaveCK" Text="Cập nhật" 
                                onclick="cmdSaveCK_Click" />
                            &nbsp;<asp:Button ID="cmdBoCK" runat="server" onclick="cmdBoCK_Click" 
                                Text="Hủy bỏ" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td valign="top" colspan="2" align="center">
                <br />
                <asp:Button runat="server" ID="cmdSave" Text="Lưu hóa đơn" OnClick="cmdSave_Click" />
                <asp:Button runat="server" ID="cmdTimKiem" Text="Tìm kiếm" OnClick="cmdTimKiem_Click" />
            </td>
        </tr>
    </table>
</asp:Panel>
<asp:Panel runat="server" ID="pnXemHoaDon">
    <center>
        <h2>
            Chi tiết hóa đơn</h2>
        <table width="600" cellpadding="0" cellspacing="0" border="0">
            <tr>
                <td width="20" nowrap style="height: 19px">
                    &nbsp;
                </td>
                <td width="100%" class="Subhead" style="height: 19px" align="left">
                    Cửa hàng:&nbsp;<asp:Label ID="lblChiNhanh" runat="server" CssClass="LabelGachDuoi"></asp:Label>
                </td>
            </tr>
            <tr>
                <td nowrap style="height: 19px" width="20">
                    &nbsp;
                </td>
                <td class="Subhead" style="height: 19px" width="100%" align="left">
                    Hóa đơn:
                    <asp:Label ID="lblMaHoaDon" runat="server" Font-Bold="True" Font-Italic="True" ForeColor="#FF0066"></asp:Label>
                    &nbsp;<span class="Subhead">Ngày&nbsp;<asp:Label ID="lblNgay" runat="server"></asp:Label>
                    </span>
                </td>
            </tr>
            <tr>
                <td width="20" nowrap style="height: 19px" align="left">
                </td>
                <td width="100%" style="height: 19px" class="Subhead" align="left">
                    Khách hàng:
                    <asp:Label runat="server" ID="lblKhachHang" CssClass="LabelGachDuoi"></asp:Label>
                </td>
            </tr>
            <tr>
                <td nowrap style="height: 20px" width="20">
                </td>
                <td style="height: 20px" width="100%" align="left">
                    Ghi chú:
                    <asp:Label ID="lblNoiDung" runat="server" CssClass="LabelGachDuoi"></asp:Label>
                    &nbsp;
                </td>
            </tr>
        </table>
        <table class="tableDep" bordercolor="black" cellpadding="0" cellspacing="0" width="600">
            <tr style="background-color: #EFFEFF;">
                <th class="thDep">
                    <strong>STT</strong>
                </th>
                <th class="thDep">
                    Mã hàng
                </th>
                <th class="thDep">
                    <strong>Tên hàng</strong>
                </th>
                <th class="thDep">
                    <strong>Giá bán</strong>
                </th>
                <th class="thDep">
                    <strong>Chiết khấu(%)</strong>
                </th>
                <th class="thDep">
                    <strong>Thành tiền</strong>
                </th>
            </tr>
            <asp:Literal runat="server" ID="lblDetailGrid"></asp:Literal>
            <tr>
                <td class="tdDep" bgcolor="white">
                    <strong></strong>
                </td>
                <td class="tdDep" bgcolor="white">
                    <strong>Cộng</strong>
                </td>
                <td class="tdDep" bgcolor="white" colspan="3">
                    <strong></strong><strong></strong><strong></strong><strong></strong>
                </td>
                <td class="tdDep" bgcolor="white" style="text-align: right">
                    <strong>
                        <asp:Label ID="lblTongTienBangSo" runat="server" CssClass="LabelGachDuoi"></asp:Label></strong>
                </td>
            </tr>
        </table>
        <br />
        <asp:Button ID="cmdCancel" runat="server" OnClick="cmdCancel_Click" Text="Hủy hóa đơn" OnClientClick="return confirm('Huy hoa don nay?');"/>
        <asp:Button ID="cmdView" runat="server" Text="Xem -&gt; in" />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="cmdAddNew" runat="server" Text="Hóa đơn mới" OnClick="cmdAddNew_Click" />
    </center>
</asp:Panel>

<script language="javascript" type="text/javascript">

    document.getElementById('<%=txtMa.ClientID%>').focus();
    function xemHoaDon() {
        var maHoaDon = document.getElementById('<%=lblMaHoaDon.ClientID%>').innerHTML
        if (maHoaDon == "") {
            alert("Ban chua luu hoa don");
            return false;
        }
        else {
            openPageXemGiaoDich('GiaoDich/InPhieuXuatKho.aspx?MaHoaDon=<%=lblMaHoaDon.Text%>');
        }
    }
    function ckLostFocus(){
        var giaGoc = document.getElementById('<%=txtGiaGoc.ClientID%>').value;
        while (giaGoc.indexOf(',') >= 0) {
            giaGoc = giaGoc.replace(',', '');
        }
        //alert(giaGoc);
        var ck = document.getElementById('<%=txtCK.ClientID%>').value;
        while (ck.indexOf(',') >= 0) {
            ck = ck.replace(',', '');
        }
        if (ck > 100) {
            alert('Chiet khau khong the lon hon 100%');
            return false;
        }
        //alert(ck);
        var giaBan = document.getElementById('<%=txtGiaBan.ClientID%>').value;
        while (giaBan.indexOf(',') >= 0) {
            giaBan = giaBan.replace(',', '');
        }
        giaBan = giaGoc - (giaGoc * ck / 100);
        //alert(giaBan);
        giaBan = '' + giaBan;
        //alert(formatStringTien(giaBan));
        document.getElementById('<%=txtGiaBan.ClientID%>').value = formatStringTien(giaBan);
    }
    function gbLostFocus() {
        var giaGoc = document.getElementById('<%=txtGiaGoc.ClientID%>').value;
        while (giaGoc.indexOf(',') >= 0) {
            giaGoc = giaGoc.replace(',', '');
        }
        //alert(giaGoc);
        var ck = document.getElementById('<%=txtCK.ClientID%>').value;
        while (ck.indexOf(',') >= 0) {
            ck = ck.replace(',', '');
        }
        if (ck > 100) {
            alert('Chiet khau khong the lon hon 100%');
            return false;
        }
        //alert(ck);
        var giaBan = document.getElementById('<%=txtGiaBan.ClientID%>').value;
        while (giaBan.indexOf(',') >= 0) {
            giaBan = giaBan.replace(',', '');
        }
        ck = 100 * (giaGoc - giaBan) / giaGoc;
        ck = lamTronSo(ck,2)
        //alert(giaBan);
        ck = '' + ck;
        //alert(formatStringTien(giaBan));
        document.getElementById('<%=txtCK.ClientID%>').value = ck;
        document.getElementById('<%=txtGiaBan.ClientID%>').value = formatStringTien(giaBan);
    }
</script>

<asp:SqlDataSource ID="dsKhachHang" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
    ProviderName="<%$ ConnectionStrings:DBConnectionString.ProviderName %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsDetail" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
    ProviderName="<%$ ConnectionStrings:DBConnectionString.ProviderName %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="ds" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
    ProviderName="<%$ ConnectionStrings:DBConnectionString.ProviderName %>"></asp:SqlDataSource>
